import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)
const Home = () => import('@/views/home/index.vue')
const Login = () => import('@/views/login/index.vue')
const Question = () => import('@/views/question/index.vue')
const Video = () => import('@/views/video/index.vue')
const User = () => import('@/views/user/index.vue')
const UserProfile = () => import('@/views/user/profile.vue')
const UserChat = () => import('@/views/user/chat.vue')
const Article = () => import('@/views/article/index.vue')
const Tabbar = () => import('@/components/app-tabbar.vue')
const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    alias: '/home',
    components: {
      default: Home,
      bottom: Tabbar
    }
  },
  {
    path: '/question',
    components: {
      default: Question,
      bottom: Tabbar
    }
  },
  {
    path: '/video',
    components: {
      default: Video,
      bottom: Tabbar
    }
  },
  {
    path: '/user',
    components: {
      default: User,
      bottom: Tabbar
    }
  },
  {
    path: '/user/profile',
    component: UserProfile
  },
  {
    path: '/user/chat',
    component: UserChat
  },
  {
    path: '/article',
    component: Article
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(store.getters['user/getToken']){
      if(to.path.toLowerCase()==='/login'){
    next('/')
  }else{
    next()
  }
  }else{
    if(to.path.toLowerCase().startsWith('user')){
      next('/login')
    }else{
      next()
    }
  }

})
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return routerPush.call(this, location).catch(error => error)
}

export default router
